package com.peak.distribution.entity.goods;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.LazyToOne;
import org.hibernate.annotations.LazyToOneOption;

import com.peak.distribution.enums.Constants;
import com.peak.distribution.enums.GoodsImageType;
import com.peak.spring.boot.api.entity.DefaultStategyEntity;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 商品图片
 * 
 */
@Entity
@Table(name = "dis_goods_image")
@Data
@EqualsAndHashCode(callSuper = true)
public class DisGoodsImage extends DefaultStategyEntity implements Comparable<DisGoodsImage> {

	private static final long serialVersionUID = Constants.DISTRIBUTION_VERSION;

	/** 类型：列表图片,缩略图,详情介绍图片 */
	private GoodsImageType type;

	/** 大图 */
	private String imageUrl;

	/** 排序编号 */
	private Integer sortNo;

	/** 商品 */
	private DisGoods goods;

	@Enumerated(EnumType.STRING)
	public GoodsImageType getType() {
		return type;
	}

	@ManyToOne
	@JoinColumn(name = "goods_id")
	@Fetch(FetchMode.SELECT)
	@LazyToOne(LazyToOneOption.PROXY)
	public DisGoods getGoods() {
		return goods;
	}

	@Override
	public int compareTo(DisGoodsImage o) {
		if (this.type == null) {
			return 1;
		}
		if (o.type == null) {
			return -1;
		}
		int result = this.type.ordinal() - o.type.ordinal();
		if (result == 0) {
			if (this.sortNo == null) {
				return 1;
			}
			if (o.sortNo == null) {
				return -1;
			}
			result = this.sortNo.compareTo(o.sortNo);
		}
		return result;
	}
}